package ee.nutilovi.praktikaportaal.company;

import java.io.IOException;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

@SuppressWarnings("serial")
public class AddOfferServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String position = req.getParameter("position");
		long companyId = Long.parseLong(req.getParameter("company"));
		Key companyKey = KeyFactory.createKey("Company", companyId);
		Text description = new Text(req.getParameter("description"));
		
		Entity o = new Entity("Offer", companyKey);
		o.setProperty("position", position);
		o.setProperty("description", description);
		o.setProperty("status", "pending");
		
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		ds.put(o);
		
		// Refresh stale cache.
		MemcacheService mc = MemcacheServiceFactory.getMemcacheService();
		mc.delete("offers");

		resp.sendRedirect("/offers");
	}
}
